package org.eyeTopLight.service.impl;

import org.eyeTopLight.dao.SunglassesGoodsDao;
import org.eyeTopLight.exception.ErrorCode;
import org.eyeTopLight.exception.ExceptionHandler;
import org.eyeTopLight.po.FrameGoods;
import org.eyeTopLight.po.SunglassesGoods;
import org.eyeTopLight.po.initEntity.Goods;
import org.eyeTopLight.service.SunglassesGoodsService;
import org.eyeTopLight.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class SunglassesGoodsServiceImpl implements SunglassesGoodsService {

    public static final Logger logger = LoggerFactory.getLogger(SunglassesGoodsServiceImpl.class);

    @Resource
    SunglassesGoodsDao sunglassesGoodsDao;


    @Override
    public SunglassesGoods getGoodsByPK(Integer goods_id){

        SunglassesGoods searchGoods = new SunglassesGoods();
        searchGoods.setSunglassesGoods_id(goods_id);

        SunglassesGoods goods =null;
        try{
            goods = sunglassesGoodsDao.queryByPK(searchGoods);
        }catch (Exception e) {
            logger.error("查询数据库frameGoods表操作失败",e);
            ExceptionHandler.processError(ErrorCode.ERR_DB_SELECT.getValue(),"查询数据库goods表操作失败");
        }
        return goods;
    }

    @Override
    public List<GoodsVo> getGoods(String goods_category, Integer startIndex, Integer maxRows){
        SunglassesGoods searchGoods = new SunglassesGoods();
        if (!goods_category.equals("全部")){
            searchGoods.setSunglassesGoods_category(goods_category);
        }
        List<GoodsVo> goodsList = new LinkedList<>();
        try{
            goodsList = sunglassesGoodsDao.queryForPage(searchGoods, startIndex, maxRows);
        }catch (Exception e){
            logger.error("查询数据库frameGoods表操作失败",e);
            ExceptionHandler.processError(ErrorCode.ERR_DB_SELECT.getValue(),"查询数据库goods表操作失败");
        }
        return goodsList;
    }

    @Override
    public SunglassesGoods getOneGood(String goods_symbol, Integer goods_id) {
        SunglassesGoods sunglassesGoods = null;

        SunglassesGoods searchGoods = new SunglassesGoods();
        searchGoods.setSunglassesGoods_id(goods_id);
        try{
            sunglassesGoods = sunglassesGoodsDao.queryByPK(searchGoods);
        }
        catch (Exception e){
            logger.error("查询数据库lenGoods表操作失败",e);
            ExceptionHandler.processError(ErrorCode.ERR_DB_SELECT.getValue(),"查询数据库lenGoods表操作失败");
        }
        return sunglassesGoods;
    }

    @Override
    public List<GoodsVo> getTopNGoods(Integer count) {
        List<GoodsVo> topNSunglasses = sunglassesGoodsDao.queryTopN(count);
        return topNSunglasses;
    }

}
